﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using ReadyApps.Common;
using ReadyApps.Common.Exceptions;
using ReadyApps.Data;
using ReadyApps.Data.Wiki;
using ReadyApps.Wiki.ViewModels;
using ReadyApps.Wiki.Views;

namespace ReadyApps.Wiki
{
    class WikiController
    {
        #region Constants

        // Resource File Keys
        public const string MANAGED_LINK_CSS_FILE = "ReadyApps.Resources.ManagedLink.css";
        public const string MANAGED_LINK_IMAGE_FILE = "ReadyApps.Resources.ManagedLink.png";
        public const string CKEDITOR_URL_REDIRECT_FILE = "ReadyApps.Resources.ckeditor_urls.js";

        // Control IDs
        public const string CONTENT_CONTROL_ID = "contentPlaceHolder";
        public const string CONTROLS_CONTROL_ID = "controlsPlaceHolder";
        public const string REVISION_INFO_CONTROL_ID = "revisionInfo"; // TODO: Implement revision info templating
        public const string BUTTON_CONTROL_ID = "buttonControl";

        // Static elements
        public const string BUTTON_SPACER = "&nbsp;&nbsp;";

        // Internal constants
        internal const int IMPOSSIBLE_PAGE_ID = -1;
        internal const int IMPOSSIBLE_FOLDER_ID = -1;
        internal const int HEAD_REVISION = -1;
        internal const int PENDING_REVISION = -2;

        private static readonly Regex clientSplitPattern =
            new Regex(@"(<a +href=""[^""]+"" +pageid=""\d+"">[^<]+</a>)|(<a +href=""[^""]+"" +pageid=""\d+"" +target=""[^""]+"">[^<]+</a>)", RegexOptions.Compiled);
        private static readonly Regex clientLinkPattern =
            new Regex(@"<a +href="".+"" +pageid=""(?<pageid>\d+)""( +target=""(?<target>.+)"")?>(?<text>.+)</a>", RegexOptions.Compiled);

        #endregion

        #region Variables

        private IWikiPageRequest pageRequest;
        private IWikiPageState pageState;
        private IWikiDataProvider dataProvider;
        private IWikiUrlManager urlManager;
        private IWikiSecurityManager securityManager;
        private ControlCollection controls;
        private WikiControlSettings controlSettings;
        private PageDetails pageDetails;

        #endregion

        #region Constructor

        /// <summary>
        /// Creates a new instance of a <see cref="WikiController"/> object. The
        /// <see cref="WikiController"/>'s main purpose is to manage the interaction of the
        /// ASP.Net framework, the data source, and the logic of manipulating the wiki
        /// content.
        /// </summary>
        /// <param name="wikiPageRequest">The <see cref="IWikiPageRequest"/> object representing
        /// the current request.</param>
        /// <param name="wikiPageState">The <see cref="IWikiPageState"/> object representing
        /// the current state.</param>
        /// <param name="wikiControlSettings">The <see cref="WikiControlSettings"/> object
        /// containing the settings from the <see cref="ReadyApps.Web.UI.WikiDisplay"/> object.</param>
        /// <param name="wikiDataProvider">The <see cref="IWikiDataProvider"/> that interacts
        /// with the configured data source.</param>
        /// <param name="wikiUrlManager">The <see cref="IWikiUrlManager"/> object that manages
        /// the virtual routes within the wiki system.</param>
        /// <param name="wikiRoleManager">The <see cref="IWikiRoleManager"/> object that interacts
        /// with the role system.</param>
        /// <param name="controlCollection">The <see cref="ControlCollection"/> to add generated
        /// controls to.</param>
        /// <exception cref="ArgumentNullException">The wikiPageRequest parameter is null.</exception>
        /// <exception cref="ArgumentNullException">The wikiPageState parameter is null.</exception>
        /// <exception cref="ArgumentNullException">The wikiControlSettings parameter is null.</exception>
        /// <exception cref="ArgumentNullException">The wikiDataProvider parameter is null.</exception>
        /// <exception cref="ArgumentNullException">The wikiUrlManager parameter is null.</exception>
        /// <exception cref="ArgumentNullException">The controlCollection parameter is null.</exception>
        public WikiController(IWikiPageRequest wikiPageRequest, IWikiPageState wikiPageState,
            WikiControlSettings wikiControlSettings, IWikiDataProvider wikiDataProvider, IWikiUrlManager wikiUrlManager,
            IWikiRoleManager wikiRoleManager, ControlCollection controlCollection)
        {
            TraceManager.BeginMethod(TraceComponent.Controller, "Ctor");

            if (wikiPageRequest == null)
            {
                throw new ArgumentNullException("wikiPageRequest");
            }
            if (wikiPageState == null)
            {
                throw new ArgumentNullException("wikiPageState");
            }
            if (wikiControlSettings == null)
            {
                throw new ArgumentNullException("wikiControlSettings");
            }
            if (wikiDataProvider == null)
            {
                throw new ArgumentNullException("wikiDataProvider");
            }
            if (wikiUrlManager == null)
            {
                throw new ArgumentNullException("wikiUrlManager");
            }
            if (controlCollection == null)
            {
                throw new ArgumentNullException("controlCollection");
            }
            pageRequest = wikiPageRequest;
            pageState = wikiPageState;
            controlSettings = wikiControlSettings;
            dataProvider = wikiDataProvider;
            urlManager = wikiUrlManager;
            securityManager = new WikiSecurityManager(pageRequest.CurrentUsername, dataProvider, wikiRoleManager);
            controls = controlCollection;

            TraceManager.EndMethod(TraceComponent.Controller, "Ctor");
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Creates the control tree in the supplied control collection based on the
        /// current object state.
        /// </summary>
        public void CreateControls()
        {
            TraceManager.BeginMethod(TraceComponent.Controller, "CreateControls");

            EnsurePageID();
            GetView().BuildControlTree(controls, !pageRequest.IsPostBack);

            TraceManager.EndMethod(TraceComponent.Controller, "CreateControls");
        }

        public void OnPreRender()
        {
            TraceManager.BeginMethod(TraceComponent.Controller, "OnPreRender");

            if (pageRequest.IsPostBack)
            {
                IWikiView view = GetView();
                view.BuildControlTree(controls, true);
                if (pageState.IsInitialized && !(view is IViewFiresTitleSetEvent))
                {
                    controlSettings.OnTitleSet(this, new TitleSetEventArgs(dataProvider.GetPageTitle(pageState.PageID)));
                }
            }

            TraceManager.EndMethod(TraceComponent.Controller, "OnPreRender");
        }

        #endregion

        #region Private Methods

        private void EnsurePageID()
        {
            TraceManager.BeginMethod(TraceComponent.Controller, "EnsurePageID");

            // Determine PageID by first looking at page state and then trying to retrieve
            // it from the data source based on the request URL
            if (!pageState.IsInitialized)
            {
                PageLookup lookup = dataProvider.LookupPage(
                    pageRequest.SiteName, pageRequest.GetFullPath(), pageRequest.RequestedPage);
                if (lookup.SiteFound)
                {
                    if (lookup.PageFound)
                    {
                        TraceManager.WriteFormat(TraceComponent.Controller, TraceLevel.Info, "Page ID {0} found", lookup.PageID);
                        pageState.Initialize(lookup.PageID);
                        pageDetails = lookup.Details;
                    }
                    else
                    {
                        pageState.InitializePageNotFound();
                    }
                }
                else
                {
                    pageState.InitializeSiteNotFound();
                }
            }
            else if (pageState.PageFound)
            {
                pageDetails = dataProvider.GetPageDetails(pageState.PageID);
            }

            TraceManager.EndMethod(TraceComponent.Controller, "EnsurePageID");
        }

        private IWikiView GetView()
        {
            TraceManager.BeginMethod(TraceComponent.Controller, "GetView");

            IWikiView view;

            // If the page state is not initialized before this method, something is wrong with the control flow
            Debug.Assert(pageState.IsInitialized, "Page state not initialized before calling GetView");
            Debug.Assert(pageDetails != null, "PageDetails is null in call to GetView");

            if (pageState.PageFound)
            {
                securityManager.DeterminePagePermissions(pageDetails.Secured, pageState.PageID);
                if (pageState.PageMode == WikiPageMode.Unknown)
                {
                    pageState.ChangePageMode(SelectPageMode(securityManager, pageDetails));
                }

                // Check authorization and display the proper view
                if (securityManager.IsWikiPageModeAuthorized(pageState.PageMode))
                {
                    TraceManager.VariableValue(TraceComponent.Controller, "PageMode", pageState.PageMode);

                    switch (pageState.PageMode)
                    {
                        case WikiPageMode.ViewPage:
                            view = GenerateNormalView();
                            break;
                        case WikiPageMode.EditPage:
                            view = GenerateEditView();
                            break;
                        case WikiPageMode.CreatePage:
                            view = GenerateNewPageView();
                            break;
                        case WikiPageMode.EditPermissions:
                            view = GenerateEditPermissionsView();
                            break;
                        case WikiPageMode.ViewRevisionList:
                            view = GenerateRevisionListView();
                            break;
                        case WikiPageMode.ViewPageRevision:
                            view = GenerateRevisionView();
                            break;
                        case WikiPageMode.ViewPagePending:
                            view = GeneratePendingView();
                            break;
                        case WikiPageMode.PageSettings:
                            view = GeneratePageSettingsView();
                            break;
                        default:
                            throw new InvalidEnumerationValueException(typeof(WikiPageMode), pageState.PageMode);
                    }
                }
                else
                {
                    view = GenerateNotAuthorizedView();
                }
            }
            else if (pageState.SiteFound) // Page not found, but site does exist
            {
                view = GenerateNotFoundView();
            }
            else // Site not found, let user create one
            {
                view = GenerateNewSiteView();
            }

            TraceManager.EndMethod(TraceComponent.Controller, "GetView");
            return view;
        }

        private static WikiPageMode SelectPageMode(IWikiSecurityManager securityManager, PageDetails details)
        {
            WikiPageMode mode = WikiPageMode.ViewPage;

            if (details.Publishable && details.PendingRevision)
            {
                if (securityManager.IsSameUser(details.PendingRevisionAuthor))
                {
                    mode = WikiPageMode.ViewPagePending;
                }
            }

            return mode;
        }

        private static PageContent GetPageContentFromRawText(string title, string rawText)
        {
            TraceManager.BeginMethod(TraceComponent.Controller, "GetPageContentFromRawText");

            int pageLinkKey = 1;
            List<InternalPageLink> pageLinks = new List<InternalPageLink>();
            string[] contentPieces = clientSplitPattern.Split(rawText);

            StringBuilder parsedContent = new StringBuilder();
            Match pageLinkMatch;
            foreach (var piece in contentPieces)
            {
                pageLinkMatch = clientLinkPattern.Match(piece);
                if (pageLinkMatch.Success)
                {
                    string displayText = pageLinkMatch.Groups["text"].Value;
                    string pageID = pageLinkMatch.Groups["pageid"].Value;
                    string target = pageLinkMatch.Groups["target"].Value;
                    pageLinks.Add(new InternalPageLink(int.Parse(pageID), pageLinkKey, string.Empty, displayText, target));
                    parsedContent.Append(string.Format("{{[link:{0}]}}", pageLinkKey));
                    pageLinkKey++;
                }
                else
                {
                    parsedContent.Append(piece);
                }
            }

            TraceManager.EndMethod(TraceComponent.Controller, "GetPageContentFromRawText");
            return new PageContent(title, parsedContent.ToString(), pageLinks);
        }

        #endregion

        #region View Generation Methods

        /// <summary>
        /// Creates a control hierarchy for the normal content view of the wiki control.
        /// </summary>
        private IWikiView GenerateNormalView()
        {
            TraceManager.BeginMethod(TraceComponent.Controller, "GenerateNormalView");

            NormalView view = new NormalView(pageState.PageID, pageDetails, controlSettings,
                dataProvider, urlManager, securityManager);
            view.EditButtonClick += new EventHandler(WikiContent_Edit_Click);
            view.SettingsButtonClick += new EventHandler(WikiContent_Settings_Click);
            view.EditPermissionsButtonClick += new EventHandler(WikiContent_EditPermissions_Click);
            view.ViewRevisionsButtonClick += new EventHandler(WikiContent_RevisionList_Click);
            view.ViewPendingButtonClick += new EventHandler(WikiContent_Pending_Click);
            view.CreatePageButtonClick += new EventHandler(WikiContent_CreatePage_Click);

            TraceManager.EndMethod(TraceComponent.Controller, "GenerateNormalView");
            return view;
        }

        /// <summary>
        /// Creates a control hierarchy for the editing mode of the wiki control.
        /// </summary>
        private IWikiView GenerateEditView()
        {
            TraceManager.BeginMethod(TraceComponent.Controller, "GenerateEditView");

            EditView view = new EditView(pageState.PageID, pageRequest.BaseFolder, pageDetails, controlSettings, dataProvider, urlManager, securityManager);
            view.CancelButtonClick += new EventHandler(EditPage_Cancel_Click);
            view.SaveButtonClick += new EventHandler<SavePageEventArgs>(EditPage_Save_Click);
            view.SavePublishButtonClick += new EventHandler<SavePageEventArgs>(EditPage_SavePublish_Click);

            TraceManager.EndMethod(TraceComponent.Controller, "GenerateEditView");
            return view;
        }

        /// <summary>
        /// Creates a control hierarchy for a missing wiki page.
        /// </summary>
        private IWikiView GenerateNotFoundView()
        {
            TraceManager.ExecuteMethod(TraceComponent.Controller, "GenerateNotFoundView");

            return new NotFoundView(controlSettings);
        }

        /// <summary>
        /// Creates a control hierarchy for an unauthorized user access.
        /// </summary>
        private IWikiView GenerateNotAuthorizedView()
        {
            TraceManager.ExecuteMethod(TraceComponent.Controller, "GenerateNotAuthorizedView");

            return new NotAuthorizedView(controlSettings);
        }

        /// <summary>
        /// Creates a control hierarchy for editing the wiki permissions.
        /// </summary>
        private IWikiView GenerateEditPermissionsView()
        {
            TraceManager.BeginMethod(TraceComponent.Controller, "GenerateEditPermissionsView");

            EditPermissions viewModel = pageState.ViewModel as EditPermissions;
            if (pageState.ViewModel == null)
            {
                pageState.ViewModel = viewModel = dataProvider.GetEditPermissions(pageState.PageID);
            }
            EditPermissionsView view = new EditPermissionsView(controlSettings, viewModel);
            view.CancelButtonClick += new EventHandler(EditPermissions_Cancel_Click);
            view.SaveButtonClick += new EventHandler(EditPermissions_Save_Click);
            view.RemoveSpecificButtonClick += new EventHandler(EditPermissions_RemoveSpecific_Click);

            TraceManager.EndMethod(TraceComponent.Controller, "GenerateEditPermissionsView");
            return view;
        }

        private IWikiView GenerateRevisionListView()
        {
            TraceManager.BeginMethod(TraceComponent.Controller, "GenerateRevisionListView");

            RevisionListView view = new RevisionListView(pageState.PageID, controlSettings, dataProvider);
            view.NormalViewButtonClick += new EventHandler(RevisionList_NormalView_Click);
            view.ViewRevisionButtonClick += new EventHandler<ViewRevisionEventArgs>(RevisionList_ViewRevision_Click);

            TraceManager.EndMethod(TraceComponent.Controller, "GenerateRevisionListView");
            return view;
        }

        private IWikiView GenerateRevisionView()
        {
            TraceManager.BeginMethod(TraceComponent.Controller, "GenerateRevisionView");

            RevisionView view = new RevisionView(pageState.PageID, pageState.RevisionNumber, controlSettings, dataProvider, urlManager);
            view.ViewRevisionsButtonClick += new EventHandler(PageRevision_RevisionList_Click);
            view.CloneToHeadButtonClick += new EventHandler(PageRevision_CloneToHead_Click);

            TraceManager.EndMethod(TraceComponent.Controller, "GenerateRevisionView");
            return view;
        }

        private IWikiView GenerateNewSiteView()
        {
            TraceManager.BeginMethod(TraceComponent.Controller, "GenerateNewSiteView");

            NewSiteView view = new NewSiteView(pageRequest.SiteName, pageRequest.BaseFolder, controlSettings,
                dataProvider, securityManager);
            view.CreateButtonClick += new EventHandler<CreateNewSiteEventArgs>(NewSite_Create_Click);

            TraceManager.EndMethod(TraceComponent.Controller, "GenerateNewSiteView");
            return view;
        }

        private IWikiView GenerateNewPageView()
        {
            TraceManager.BeginMethod(TraceComponent.Controller, "GenerateNewPageView");

            NewPageView view = new NewPageView(pageState.PageID, pageRequest.BaseFolder, controlSettings, dataProvider, urlManager, securityManager);
            view.CancelButtonClick += new EventHandler(NewPage_Cancel_Click);
            view.SaveButtonClick += new EventHandler<SavePageEventArgs>(NewPage_Save_Click);

            TraceManager.EndMethod(TraceComponent.Controller, "GenerateNewPageView");
            return view;
        }

        private IWikiView GeneratePageSettingsView()
        {
            TraceManager.BeginMethod(TraceComponent.Controller, "GeneratePageSettingsView");

            PageSettingsViewModel viewModel = pageState.ViewModel as PageSettingsViewModel;
            if (viewModel == null)
            {
                pageState.ViewModel = viewModel = new PageSettingsViewModel(
                    securityManager.CanManagePublishing,
                    securityManager.CanManageRevisions,
                    securityManager.CanManagePublishing ? dataProvider.GetPublicationSettings(pageState.PageID) : null,
                    securityManager.CanManageRevisions ? dataProvider.GetRetentionSettings(pageState.PageID) : null);
            }
            PageSettingsView view = new PageSettingsView(controlSettings, viewModel);
            view.CancelButtonClick += new EventHandler(PageSettings_Cancel_Click);
            view.SaveButtonClick += new EventHandler<SaveViewModelEventArgs>(PageSettings_Save_Click);

            TraceManager.EndMethod(TraceComponent.Controller, "GeneratePageSettingsView");
            return view;
        }

        private IWikiView GeneratePendingView()
        {
            TraceManager.BeginMethod(TraceComponent.Controller, "GeneratePendingView");

            PendingView view = new PendingView(pageState.PageID, pageDetails, controlSettings, dataProvider, urlManager, securityManager);
            view.ViewCurrentButtonClick += new EventHandler(Pending_Current_Click);
            view.EditButtonClick += new EventHandler(Pending_Edit_Click);
            view.PublishButtonClick += new EventHandler(Pending_Publish_Click);
            view.DeleteButtonClick += new EventHandler(Pending_Delete_Click);

            TraceManager.EndMethod(TraceComponent.Controller, "GeneratePendingView");
            return view;
        }

        #endregion

        #region Event Delegates

        private void EditPage_Cancel_Click(object sender, EventArgs e)
        {
            if (pageDetails.Publishable && pageDetails.PendingRevision)
            {
                pageState.ChangePageMode(WikiPageMode.ViewPagePending);
            }
            else
            {
                pageState.ChangePageMode(WikiPageMode.ViewPage);
            }
        }

        private void EditPage_Save_Click(object sender, SavePageEventArgs e)
        {
            Debug.Assert(e.ExistingPage, "Edit Page View generated a non-existent SavePage Event Args.");

            PageContent content = GetPageContentFromRawText(e.PageTitle, e.RawContent);
            dataProvider.UpdatePage(pageState.PageID, securityManager.CurrentUsername, content, false);

            if (pageDetails.Publishable)
            {
                pageDetails.PendingRevision = true;
                pageDetails.PendingRevisionAuthor = securityManager.CurrentUsername;
                pageState.ChangePageMode(WikiPageMode.ViewPagePending);
            }
            else
            {
                pageState.ChangePageMode(WikiPageMode.ViewPage);
            }
        }

        private void EditPage_SavePublish_Click(object sender, SavePageEventArgs e)
        {
            Debug.Assert(e.ExistingPage, "Edit Page View generated a non-existent SavePage Event Args.");

            PageContent content = GetPageContentFromRawText(e.PageTitle, e.RawContent);
            dataProvider.UpdatePage(pageState.PageID, securityManager.CurrentUsername, content, true);

            pageDetails.PendingRevision = false;
            pageDetails.PendingRevisionAuthor = string.Empty;
            pageState.ChangePageMode(WikiPageMode.ViewPage);
        }

        private void EditPermissions_Cancel_Click(object sender, EventArgs e)
        {
            pageState.ChangePageMode(WikiPageMode.ViewPage);
        }

        private void EditPermissions_RemoveSpecific_Click(object sender, EventArgs e)
        {
            dataProvider.RemovePageSpecificPermissions(pageState.PageID);
            pageState.ChangePageMode(WikiPageMode.ViewPage);
        }

        private void EditPermissions_Save_Click(object sender, EventArgs e)
        {
            dataProvider.SavePagePermissions(pageState.PageID, pageState.ViewModel as EditPermissions);
            securityManager.DeterminePagePermissions(pageDetails.Secured, pageState.PageID);
            pageState.ChangePageMode(WikiPageMode.ViewPage);
        }

        private void NewSite_Create_Click(object sender, CreateNewSiteEventArgs e)
        {
            dataProvider.CreateNewSite(pageRequest.SiteName, e.SecurityEnabled, pageRequest.BaseFolder, e.Creator);
            pageState.Reset();
            EnsurePageID(); // This looks up the data and sets the appropriate state
        }

        private void NewPage_Cancel_Click(object sender, EventArgs e)
        {
            pageState.ChangePageMode(WikiPageMode.ViewPage);
        }

        private void NewPage_Save_Click(object sender, SavePageEventArgs e)
        {
            Debug.Assert(!e.ExistingPage, "New Page View generated an existing page SavePage Event Args.");

            PageContent content = GetPageContentFromRawText(e.PageTitle, e.RawContent);
            dataProvider.CreateNewPage(pageRequest.SiteName, pageRequest.GetFullPath(), e.PageName,
                pageRequest.CurrentUsername, content);

            controlSettings.RequestRedirect(urlManager.GetVirtualUrl(pageRequest.GetFullPath() + e.PageName));
        }

        private void PageRevision_RevisionList_Click(object sender, EventArgs e)
        {
            pageState.ChangePageMode(WikiPageMode.ViewRevisionList);
            pageState.RevisionNumber = HEAD_REVISION;
        }

        private void PageRevision_CloneToHead_Click(object sender, EventArgs e)
        {
            dataProvider.CloneRevisionToHead(pageState.PageID, pageState.RevisionNumber, pageRequest.CurrentUsername);
            pageState.RevisionNumber = HEAD_REVISION;
            pageState.ChangePageMode(WikiPageMode.ViewPage);
        }

        private void PageSettings_Cancel_Click(object sender, EventArgs e)
        {
            pageState.ChangePageMode(WikiPageMode.ViewPage);
        }

        private void PageSettings_Save_Click(object sender, SaveViewModelEventArgs e)
        {
            PageSettingsViewModel settings = e.ViewModel as PageSettingsViewModel;

            Debug.Assert(e.ViewModel != null, "PageSettings Save event created incorrect type of ViewModel");

            if (settings.CanManagePublishing)
            {
                dataProvider.SavePublicationSettings(pageState.PageID, settings.PublicationSettings);
                pageDetails.Publishable = settings.PublicationSettings.Publishable;
            }
            if (settings.CanManageRevisions)
            {
                dataProvider.SaveRetentionSettings(settings.RetentionSettings);
                pageDetails.Versioned = settings.RetentionSettings.Unit != RetentionUnit.None;
            }
            pageState.ChangePageMode(WikiPageMode.ViewPage);
        }

        private void Pending_Current_Click(object sender, EventArgs e)
        {
            pageState.ChangePageMode(WikiPageMode.ViewPage);
        }

        private void Pending_Delete_Click(object sender, EventArgs e)
        {
            dataProvider.DeletePendingRevision(pageState.PageID);
            pageState.ChangePageMode(WikiPageMode.ViewPage);
            pageDetails.PendingRevision = false;
        }

        private void Pending_Edit_Click(object sender, EventArgs e)
        {
            pageState.ChangePageMode(WikiPageMode.EditPage);
        }

        private void Pending_Publish_Click(object sender, EventArgs e)
        {
            dataProvider.PublishPage(pageState.PageID);
            pageState.ChangePageMode(WikiPageMode.ViewPage);
            pageDetails.PendingRevision = false;
        }

        private void RevisionList_NormalView_Click(object sender, EventArgs e)
        {
            pageState.ChangePageMode(WikiPageMode.ViewPage);
        }

        private void RevisionList_ViewRevision_Click(object sender, ViewRevisionEventArgs e)
        {
            pageState.ChangePageMode(WikiPageMode.ViewPageRevision);
            pageState.RevisionNumber = e.RevisionNumber;
        }

        private void WikiContent_CreatePage_Click(object sender, EventArgs e)
        {
            pageState.ChangePageMode(WikiPageMode.CreatePage);
        }

        private void WikiContent_Edit_Click(object sender, EventArgs e)
        {
            pageState.ChangePageMode(WikiPageMode.EditPage);
        }

        private void WikiContent_EditPermissions_Click(object sender, EventArgs e)
        {
            pageState.ChangePageMode(WikiPageMode.EditPermissions);
        }

        private void WikiContent_Pending_Click(object sender, EventArgs e)
        {
            pageState.ChangePageMode(WikiPageMode.ViewPagePending);
        }

        private void WikiContent_RevisionList_Click(object sender, EventArgs e)
        {
            pageState.ChangePageMode(WikiPageMode.ViewRevisionList);
        }

        private void WikiContent_Settings_Click(object sender, EventArgs e)
        {
            pageState.ChangePageMode(WikiPageMode.PageSettings);
        }

        #endregion
    }
}
